package com.alibaba.citrus.ots.platform.enhance.functions.strategy.entity;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.repository.DimensionWriteRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.StrategyWriteRepository;
import com.epoch.app.otsplatformenhance.sdo.DimensionSDO;
import com.epoch.app.otsplatformenhance.sdo.StrategySDO;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

@Getter
public class StrategyEntity {

    private StrategySDO strategySDO;

    private static StrategyWriteRepository strategyWriteServiceRepository;

    private static DimensionWriteRepository dimensionWriteRepository;

    static {
        strategyWriteServiceRepository = SpringContextUtil.getBean(StrategyWriteRepository.class);
        dimensionWriteRepository = SpringContextUtil.getBean(DimensionWriteRepository.class);
    }

    public static StrategyEntity of(StrategySDO strategySDO) {
        StrategyEntity strategyEntity = new StrategyEntity();
        strategyEntity.strategySDO = strategySDO;
        return strategyEntity;
    }

    public String createAndUpdate() {
        if (null == strategySDO.getId()) {
            String id = strategyWriteServiceRepository.save(this.strategySDO);
            for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
                dimensionSDO.setStrategyId(id);
                dimensionWriteRepository.create(dimensionSDO);
            }
            return id;
        }
        String id = strategyWriteServiceRepository.modify(this.strategySDO);
        for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
            dimensionSDO.setStrategyId(id);
            if (dimensionSDO.getId() == null ) {
                dimensionWriteRepository.create(dimensionSDO);
            }else {
                dimensionWriteRepository.modify(dimensionSDO);
            }
        }
        return id;
    }

    @Transactional(rollbackFor = {RuntimeException.class,Exception.class})
    public String delete() {
        if (!CollectionUtils.isEmpty(this.strategySDO.getDimensionSDOList())) {
            this.strategySDO.getDimensionSDOList().stream().forEach(dimensionSDO -> {
                dimensionWriteRepository.delete(dimensionSDO);
            });
        }
        String id = strategyWriteServiceRepository.delete(this.strategySDO);
        return id;
    }

    public String modify() {
        String id = strategyWriteServiceRepository.modify(this.strategySDO);
        if (CollectionUtils.isNotEmpty(strategySDO.getDimensionSDOList())) {
            for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
                dimensionWriteRepository.update(dimensionSDO);
            }
        }
        return id;
    }

    public Boolean save() {
        String id = strategyWriteServiceRepository.modify(this.strategySDO);
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(strategySDO.getDimensionSDOList())) {
            for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
                dimensionWriteRepository.modify(dimensionSDO);
            }
        }
        return true;
    }

    public String createFirstReply() {
        if (null == strategySDO.getId()) {
            String id = strategyWriteServiceRepository.save(strategySDO);
            for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
                dimensionSDO.setStrategyId(id);
                dimensionWriteRepository.create(dimensionSDO);
            }
            return id;
        } else {
            String s = strategyWriteServiceRepository.modify(strategySDO);
            for (DimensionSDO dimensionSDO : strategySDO.getDimensionSDOList()) {
                dimensionSDO.setStrategyId(s);
                dimensionWriteRepository.create(dimensionSDO);
            }
            return s;
        }
    }
}
